//
//  ZJCycleImageViewCell.m
//  CycleImageScrollDemo
//
//  Created by 郑佳 on 16/6/15.
//  Copyright © 2016年 Triangle. All rights reserved.
//

#import "ZJCycleImageViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ZJCycleImageModel.h"

CGFloat const kTitleLeftMargin = 15;

@interface ZJCycleImageViewCell ()

/** 轮播图的*/
@property (nonatomic, strong) UIImageView *imageView;
/** 标题 */
@property (nonatomic, strong) UILabel *title;
/** 详细描述*/
@property (nonatomic, strong) UILabel *detailTitle;
@property (nonatomic, assign) CGRect frame;

@end

@implementation ZJCycleImageViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _title = [[UILabel alloc]init];
    _title.numberOfLines = 2;
    _detailTitle = [[UILabel alloc]init];
    _detailTitle.numberOfLines = 3;
    _imageView = [[UIImageView alloc]init];
    
    [self.contentView addSubview:_imageView];
    [self.contentView addSubview:_title];
    [self.contentView addSubview:_detailTitle];
    
}


- (void)setLayout {
    
    _imageView.frame = self.bounds;
    _title.frame = CGRectMake(kTitleLeftMargin,_frame.size.height - 50,_frame.size.width - kTitleLeftMargin * 2, 30);
    
    if (self.style == ZJCycleImageViewStyleSpecial) {
        
        _imageView.frame = CGRectMake(0, 0, _frame.size.width, _frame.size.height * 0.56);
        
        CGSize titleSize = [self resetlableSize:self.title fontSize:17];
        
        [self.title setFrame:CGRectMake(kTitleLeftMargin,CGRectGetMaxY(_imageView.frame) + 12,_frame.size.width - kTitleLeftMargin * 2, titleSize.height)];
        
        CGSize detailTitleSize = [self resetlableSize:self.detailTitle fontSize:13];
        [self.detailTitle setFrame:CGRectMake(kTitleLeftMargin, CGRectGetMaxY(_title.frame) + 8.5,_frame.size.width - kTitleLeftMargin * 2, detailTitleSize.height)];
        
    }
}

- (void)setModel:(ZJCycleImageModel *)model {
    _model = model;
    
    [self setData];
    [self setLayout];
    
}

- (void)setData {
    
    [_imageView sd_setImageWithURL:[NSURL URLWithString:self.model.imageURL] placeholderImage:[UIImage imageNamed:@"pic_default_750x400"]];
    self.title.text = self.model.title;
    self.detailTitle.text = self.model.detailTitle;

}

- (CGSize)resetlableSize:(UILabel *)label fontSize:(CGFloat)fontSize {

    if (label.text.length == 0 || label == nil) {
        return CGSizeZero;
    }
    //文字的字体
    NSDictionary *attributeDict = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    return [label.text boundingRectWithSize:CGSizeMake(self.frame.size.width - 2 * kTitleLeftMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributeDict context:nil].size;
}

- (void)setTitleColor:(UIColor *)color fontSize:(CGFloat)fontSize {
    self.title.textColor = color;
    self.title.font = [UIFont systemFontOfSize:fontSize];
}

- (void)setDetailTitleColor:(UIColor *)color fontSize:(CGFloat)fontSize {
    self.detailTitle.textColor = color;
    self.detailTitle.font = [UIFont systemFontOfSize:fontSize];
}

@end
